package zengjin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import zengjin.common.BeanCopyUtil;
import zengjin.common.PageResponse;
import zengjin.mapper.ReportMapper;
import zengjin.model.dto.report.AddReportRequest;
import zengjin.model.dto.report.PageReportRequest;
import zengjin.model.dto.report.UpdateReportRequest;
import zengjin.model.en.Report;
import zengjin.model.vo.report.PageReportVO;
import zengjin.model.vo.report.ReportVO;
import zengjin.service.ReportService;

import java.util.List;

/**
* @author 19324
* @description 针对表【report】的数据库操作Service实现
* @createDate 2025-03-27 09:53:56
*/
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report>
    implements ReportService {

    @Resource
    private ReportMapper reportMapper;

    @Override
    public boolean addReport(AddReportRequest addReportRequest) {
        //1.获取参数
        String reportCheckStatus = addReportRequest.getReportCheckStatus();
        Long reportUserId = addReportRequest.getReportUserId();
        String reportUserRole = addReportRequest.getReportUserRole();
        String reportUsername = addReportRequest.getReportUsername();
        String reportAccount = addReportRequest.getReportAccount();
        String reportContact = addReportRequest.getReportContact();
        Long reportObjectId = addReportRequest.getReportObjectId();
        String reportObject = addReportRequest.getReportObject();
        String reportDescribe = addReportRequest.getReportDescribe();
        List<String> reportFileList = addReportRequest.getReportFileList();

        String result = String.join(",", reportFileList);
        //2.封装对象
        Report report = new Report();
        report.setReportCheckStatus(reportCheckStatus);
        report.setReportUserId(reportUserId);
        report.setReportUserRole(reportUserRole);
        report.setReportUsername(reportUsername);
        report.setReportAccount(reportAccount);
        report.setReportContact(reportContact);
        report.setReportObjectId(reportObjectId);
        report.setReportObject(reportObject);
        report.setReportDescribe(reportDescribe);
        report.setReportFileList(result);

        //3.插入数据库
        return reportMapper.insert(report) > 0;
    }

    @Override
    public boolean updateReport(UpdateReportRequest updateReportRequest) {
        //1.获取参数
        Long reportId = updateReportRequest.getReportId();
        String reportCheckStatus = updateReportRequest.getReportCheckStatus();
        String reportCheckRemark = updateReportRequest.getReportCheckRemark();
        String reportDescribe = updateReportRequest.getReportDescribe();
        String reportFileList = updateReportRequest.getReportFileList();

        //2.构建更新条件
        LambdaUpdateWrapper<Report> updateWrapper = new UpdateWrapper<Report>().lambda()
                .eq(reportId != null, Report::getReportId, reportId)
                .set(StrUtil.isNotBlank(reportCheckStatus), Report::getReportCheckStatus, reportCheckStatus)
                .set(StrUtil.isNotBlank(reportCheckRemark), Report::getReportCheckRemark, reportCheckRemark)
                .set(StrUtil.isNotBlank(reportDescribe), Report::getReportDescribe, reportDescribe)
                .set(StrUtil.isNotBlank(reportFileList), Report::getReportFileList, reportFileList);
        return reportMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public PageResponse<PageReportVO> pageQueryReport(PageReportRequest pageReportRequest) {
        //1.获取参数
        String reportCheckStatus = pageReportRequest.getReportCheckStatus();
        String reportObject = pageReportRequest.getReportObject();
        String reportUserId = pageReportRequest.getReportUserId();


        //2.构建查询条件
        Page<Report> page = pageReportRequest.toPageDefaultSortByUpdateTime();

        //3.分页查询
        Page<Report> reportPage =lambdaQuery()
                .eq(StrUtil.isNotBlank(reportCheckStatus), Report::getReportCheckStatus, reportCheckStatus)
                .eq(StrUtil.isNotBlank(reportObject), Report::getReportObject, reportObject)
                .eq(StrUtil.isNotBlank(reportUserId), Report::getReportUserId, reportUserId)
                .page(page);
        return PageResponse.of(reportPage, PageReportVO.class);
    }

    @Override
    public boolean deleteReport(Long reportId) {
        return reportMapper.deleteById(reportId) > 0;
    }

    @Override
    public ReportVO queryReportDetail(Long reportId) {
        Report report = reportMapper.selectById(reportId);
        return BeanUtil.copyProperties(report, ReportVO.class);
    }


}




